package org.grow.jssp.service;

/**
 * @Author: xwg
 * @CreateDate: 2022/3/14
 */

import org.grow.jssp.entity.JsspFile;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
/** lambda 表达式 又叫 箭头函数 针对list处理*/
public class FileLambdaOps implements FileOperator {

    @Override
    public List<JsspFile> ops(String filename) {
//      File -> jdk7 -> Path Paths Files
//  /xxx/dds/ddd
        Path path = Paths.get(filename);
        try {
            Stream<Path> allFiles = Files.list(path);

            System.out.println("==============");
//            System.out.println(allFiles.collect(Collectors.toList()));

//            过滤出 映射
    /*        Stream<Path> pathStream = allFiles.filter((e) -> {
                return Files.isRegularFile(e);
            });

            Stream<JsspFile> jsspFileStream = pathStream.map((e) -> {
                JsspFile jsspFile = new JsspFile();
                try {
                    jsspFile.setSize((int)Files.size(e));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                jsspFile.setTitle(e.getFileName().toString());
                return jsspFile;
            });*/
//            System.out.println(jsspFileStream.collect(Collectors.toList()));
            List<JsspFile> collect = allFiles.filter((e) -> {
                return Files.isRegularFile(e);
            }).map((e) -> {
                JsspFile jsspFile = new JsspFile();
                try {
                    jsspFile.setSize((int) Files.size(e));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                jsspFile.setTitle(e.getFileName().toString());
                return jsspFile;
            }).collect(Collectors.toList());
            System.out.println(collect);
            return collect;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
